package com.lagou.edu.controller;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;

/**
 * @author 赵虎
 * 2020/10/24 2:30 上午
 */
@Controller
public class LoginController {

    @RequestMapping("/login")
    public String login(String error, Model model) {
        model.addAttribute("error", error);
        return "login";
    }

    @RequestMapping("/doLogin")
    public String doLogin(String username, String password, RedirectAttributes attr, HttpServletRequest request) {
        if (StringUtils.isEmpty(username) || StringUtils.isEmpty(password) || !username.equals("admin") || !password.equals("admin")) {
            attr.addAttribute("error", "用户名或密码错误");
            return "redirect:/login";
        } else {
            request.getSession().setAttribute("user", username);
            return "redirect:/resume";
        }
    }
}
